from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

from usermanagement.models import UserProfile
from django.contrib.admin.filters import SimpleListFilter
 
admin.site.unregister(User)

class AcceptedRulesFilter(SimpleListFilter):
    # human-readable title
    title = 'Regeln akzeptiert'
    
    # parameter for filter in url
    parameter_name = 'accepted_rules'
    
    def lookups(self, request, model_admin):
        """
        Basically a boolean type
        This gives the options given in the interface
        """
        return (
                ('True', 'Ja'),
                ('False', 'Nein')
                )
    
    def queryset(self, request, queryset):
        """
        Return the filtered queryset
        """
        if self.value() == 'True':
            return queryset.filter(pk__in=UserProfile.objects.filter(accepted_rules=True).values_list('user', flat=True))
        if self.value() == 'False':
            return queryset.filter(pk__in=UserProfile.objects.filter(accepted_rules=False).values_list('user', flat=True))
        
        return queryset
 
class UserProfileInline(admin.StackedInline):
    model = UserProfile
 
class UserProfileAdmin(UserAdmin):
    inlines = [UserProfileInline]
    list_filter = (AcceptedRulesFilter, )
 
admin.site.register(User, UserProfileAdmin)
